import java.io.*;
import java.nio.file.*;
import java.util.*;

/**
 * A command-line tool that simulates the Unix 'tail' command
 * Displays the last lines of a file (default: 10 lines)
 */
public class Tail {
    private static final int DEFAULT_LINES = 10;
    
    public static void main(String[] args) {
        if (args.length == 0) {
            printUsage();
            return;
        }
        
        try {
            if (args[0].equals("-n") && args.length >= 3) {
                // tail -n <number> <file>
                int numLines = Integer.parseInt(args[1]);
                String filename = args[2];
                displayTail(filename, numLines);
            } else if (args[0].startsWith("-") && args[0].length() > 1) {
                // tail -<number> <file>
                int numLines = Integer.parseInt(args[0].substring(1));
                String filename = args[1];
                displayTail(filename, numLines);
            } else {
                // tail <file>
                String filename = args[0];
                displayTail(filename, DEFAULT_LINES);
            }
        } catch (NumberFormatException e) {
            System.err.println("Error: Invalid number format");
            printUsage();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
    
    private static void displayTail(String filename, int numLines) throws IOException {
        Path filePath = Paths.get(filename);
        
        if (!Files.exists(filePath)) {
            System.err.println("tail: cannot open '" + filename + "' for reading: No such file or directory");
            return;
        }
        
        if (!Files.isRegularFile(filePath)) {
            System.err.println("tail: error reading '" + filename + "': Is a directory");
            return;
        }
        
        // Read all lines first, then display the last numLines
        List<String> allLines = new ArrayList<>();
        
        try (BufferedReader reader = Files.newBufferedReader(filePath)) {
            String line;
            while ((line = reader.readLine()) != null) {
                allLines.add(line);
            }
        }
        
        // Calculate starting index for the last numLines
        int totalLines = allLines.size();
        int startIndex = Math.max(0, totalLines - numLines);
        
        // Print the last numLines
        for (int i = startIndex; i < totalLines; i++) {
            System.out.println(allLines.get(i));
        }
    }
    
    private static void printUsage() {
        System.out.println("Usage: java Tail [OPTION]... [FILE]...");
        System.out.println("Print the last 10 lines of each FILE to standard output.");
        System.out.println();
        System.out.println("Options:");
        System.out.println("  -n NUM       print the last NUM lines instead of the last 10");
        System.out.println("  -NUM         same as -n NUM");
        System.out.println();
        System.out.println("Examples:");
        System.out.println("  java Tail file.txt           display last 10 lines of file.txt");
        System.out.println("  java Tail -n 5 file.txt      display last 5 lines of file.txt");
        System.out.println("  java Tail -5 file.txt        display last 5 lines of file.txt");
    }
}